package com.example.qx;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.provider.ContactsContract;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import androidx.appcompat.app.AppCompatActivity;

import com.example.qx.database.data;
import com.example.qx.lei.leilianxr;

import net.sourceforge.pinyin4j.PinyinHelper;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.CountDownLatch;

public class zc extends AppCompatActivity {

    private String mima1,mima,mingzi,zhh;
    private int zhanghao;
    private EditText zhang,mi1,mi,mz;
    private Button butwc;
    private data data= new data();

    public zc() throws SQLException, ClassNotFoundException, IllegalAccessException, InstantiationException {
    }

    @Override
    protected  void onCreate(Bundle a){
        super.onCreate(a);
        setContentView(R.layout.zc);
        zhang=findViewById(R.id.zczhanghao);
        mi1=findViewById(R.id.zc1mima);
        mi=findViewById(R.id.zcmima);
        butwc=findViewById(R.id.zc1);
        mz=findViewById(R.id.zcmingzi);


        butwc.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                zhh=zhang.getText().toString();
                zhanghao=Integer.parseInt(zhh);
                mima=mi.getText().toString();
                mima1=mi1.getText().toString();
                mingzi=mz.getText().toString();
                if(Objects.equals(zhh, "")|| Objects.equals(mima, "") || Objects.equals(mima1, ""))
                    display2();
                else if(!Objects.equals(mima, mima1))
                    display();
                else if(Objects.equals(mima, mima1) && !Objects.equals(mima, ""))
                {
                    final Connection[] connection = new Connection[1];
                    CountDownLatch latch=new CountDownLatch(1);
                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                            try {Class.forName("com.mysql.jdbc.Driver").newInstance();}
                            catch (IllegalAccessException | ClassNotFoundException |
                                   InstantiationException e) {
                                throw new RuntimeException(e);}
                            try {
                                connection[0] = DriverManager.getConnection("jdbc:mysql://sh-cdb-9761pjk2.sql.tencentcdb.com:29081/youqi","root","asd123456");

                                int a= 0;
                                try {
                                    a = data.hdzhanghao(zhanghao,connection[0]);
                                } catch (SQLException   e) {
                                    throw new RuntimeException(e);
                                }
                                if(a==0)
                                {
                                    try {
                                        data.insertzhangmi(zhanghao,mima,mingzi,connection[0]);
                                    } catch (SQLException e) {
                                        throw new RuntimeException(e);
                                    }
                                    display1();}
                                if(a==1)
                                    display3();
                            } catch (SQLException e) {throw new RuntimeException(e);}
                            latch.countDown();
                        }
                    }).start();
                    try{
                        latch.await();
                        connection[0].close();
                    }
                    catch (InterruptedException | SQLException e)
                    {e.printStackTrace();}

                }

            }
        });

    }
    public void display(){
        AlertDialog.Builder builder=new AlertDialog.Builder(this);
        builder.setMessage("密码不一致！！！");
        AlertDialog diog=builder.create();
         diog.show();
         new Handler().postDelayed(new Runnable() {
             @Override
             public void run() {
                diog.dismiss();
             }
         },1000);

    }
    public void display1(){
        AlertDialog.Builder builder=new AlertDialog.Builder(this);

        builder.setMessage("注册成功！！！");
        AlertDialog diog=builder.create();
        diog.show();
        diog.setOnDismissListener(new DialogInterface.OnDismissListener() {
            @Override
            public void onDismiss(DialogInterface dialog) {
                Intent intent =new Intent(zc.this,main.class);
                startActivity(intent);
            }
        });
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                diog.dismiss();
            }
        },1000);

    }
    public void display3(){
        AlertDialog.Builder builder=new AlertDialog.Builder(this);
        builder.setMessage("账号已存在!!!");
        AlertDialog diog=builder.create();
        diog.show();
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                diog.dismiss();
            }
        },1000);

    }
    public void display2(){
        AlertDialog.Builder builder=new AlertDialog.Builder(this);
        builder.setMessage("请填写完整信息！！！");
        AlertDialog diog=builder.create();
        diog.show();
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                diog.dismiss();
            }
        },1000);

    }


}
